// pages/my/my.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    istokenPop: false, //判断是否等了
    params: {
      userId: wx.getStorageSync('userId')
    },

    userHeadPortraitPath: '',//头像地址
    nickName: '',//昵称
    bookDonateOrderNum: 0, //卖书数量
    trolleyNum: 0, //购物车数量
    sdxScores: 0, //书币
    userTel: '',
    orderNum: 0
  },

  // 跳转我卖书的书
  myJuanzengBook () {

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }

    // if (wx.getStorageSync('token')=='') {
    //   this.setData({
    //     istokenPop: true
    //   })
    //   return
    // }
    wx.navigateTo({
      url: '/package/my/pages/myJuanzengBook/myJuanzengBook',
    })
  },


  changeData8 () {
    return
  },

  //跳转我的书币

  myJifen () {
    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    // if (wx.getStorageSync('token')=='') {
    //   this.setData({
    //     istokenPop: true
    //   })
    //   return
    // }
    wx.navigateTo({
      url: '/package/my/pages/myJifen/myJifen?num=' + this.data.sdxScores,
    })
  },

  //跳转我的购物车

  shopCar () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/shopCar/shopCar',
    })
  },

  // 跳转我的动态
  myTrends () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/myTrends/myTrends',
    })
  },

  //跳转关注书有
  bookFriend () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/bookFriend/bookFriend',
    })
  },


  //跳转修改个人信息
  getUser () {

    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/getUser/getUser',
    })
  },

  // 跳转心愿单

  heartDanzi () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/heartDanzi/heartDanzi',
    })
  },

  //地址管理

  location_guanli () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/juanshu/pages/addYouji/addYouji?type=1' ,
    })
  },

  //我的书卷

  myShujuan () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/myShujuan/myShujuan',
    })
  },


  //我的订单
  myOrder () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/myOrder/myOrder',
    })
  },


  //跳转关于我们
  goAboutus () {
    if (wx.getStorageSync('token')=='') {
      this.setData({
        istokenPop: true
      })
      return
    }
  },


  //去做工艺
  goGongyi () {
    // if (wx.getStorageSync('token')=='') {
    //   this.setData({
    //     istokenPop: true
    //   })
    //   return
    // }
    wx.navigateToMiniProgram({
      appId: 'wx77a8c12808edf5b9',
      path: 'pages/logs/logs',
      extraData: {
        foo: 'bar'
      },
      envVersion: 'develop',
      success(res) {
        // 打开成功
      }
    })
  },



  //接口部分

  //获取用户信息
  getUserinfo (params) {
    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/user/infos/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
      "token" : wx.getStorageSync('token'),
        "uid" : wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            users: res.data.data,
            userHeadPortraitPath: res.data.data.userHeadPortraitPath,
            nickName: res.data.data.name,
            bookDonateOrderNum: res.data.data.bookDonateOrderNum,
            trolleyNum: res.data.data.trolleyNum,
            sdxScores: res.data.data.sdxScores,
            userTel: res.data.data.userTel
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },


  //好友分享

  shareFriends () {

    if (wx.getStorageSync('token') == '') {
      this.setData({
        istokenPop: true
      })
      return
    }
    wx.navigateTo({
      url: '/package/my/pages/shareFriends/shareFriends',
    })
  },


  //登陆成功之后调用

  loginCode () {
    this.setData({
      istokenPop: false
    })
    this.onShow()
  },

  copyName () {
    wx.setClipboardData({
      data: '书濑熊',
      success: function (res) {
        wx.getClipboardData({
          success: function(res) {
            console.log(res.data) // data
            wx.showToast({
              title: '公众号复制成功',
              icon: 'none'
            })
          }
        })
      }
    })
  },

  copyName2 () {
    return false
  },


  //取消弹框
  touchMove () {
    this.setData({
      istokenPop: true
    })
  },


  //获取未支付订单数量
  getDotOrder () {

    var _this = this
    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/order/purchase/findWeizhifu/auth',
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          _this.setData({
            orderNum: res.data.data
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    app.getUserset()

    // if (wx.getStorageSync('token') != '' && wx.getStorageSync('token') != null && wx.getStorageSync('token')!=undefined) {

    if (wx.getStorageSync('token') != '') {
      
        this.getUserinfo(this.params) 

        this.getDotOrder()
    }else{
      this.setData({
        userHeadPortraitPath: '',//头像地址
        nickName: '',//昵称
        bookDonateOrderNum: 0, //卖书数量
        trolleyNum: 0, //购物车数量
        sdxScores: 0, //书币
        userTel: '',
        orderNum: 0
      })
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
